package com.team.teamproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.team.teamproject.R;
import com.team.teamproject.bean.Data_HomePageBottom;

import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */
public class MyHomePageBottomAdapter extends BaseAdapter {
    private Context context;
    private List<Data_HomePageBottom.ItemsBean> items;

    public MyHomePageBottomAdapter(Context context, List<Data_HomePageBottom.ItemsBean> items) {
        this.context = context;
        this.items = items;
    }



    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_lv_homepage_bottm, null);
            holder = new ViewHolder();
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.nickname= (TextView) convertView.findViewById(R.id.nickname);
            holder.introduction= (TextView) convertView.findViewById(R.id.introduction);
            holder.ivImage= (ImageView) convertView.findViewById(R.id.iv_image);
            holder.title= (TextView) convertView.findViewById(R.id.title);
            holder.shareMsg= (TextView) convertView.findViewById(R.id.share_msg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //取值
        Data_HomePageBottom.ItemsBean itemsBean = items.get(position);
        String type = itemsBean.getType();
        //Log.i("aaa","type--->"+type);
        if (type.equals("ad")){
           //Log.i("aaa","ad-->url__>"+itemsBean.getImage_url());
            Picasso.with(context)
                    .load(itemsBean.getImage_url())
                    .into(holder.ivImage);
        }else{
            holder.nickname.setText(itemsBean.getAuthor().getNickname());
            holder.introduction.setText(itemsBean.getAuthor().getIntroduction());
            holder.title.setText(itemsBean.getTitle());
            holder.shareMsg.setText(itemsBean.getShare_msg());
            Picasso.with(context).load(itemsBean.getAuthor().getAvatar_url()).into(holder.iv);
            Picasso.with(context).load(itemsBean.getCover_webp_url()).into(holder.ivImage);
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView iv;
        TextView nickname;
        TextView introduction;
        ImageView ivImage;
        TextView title;
        TextView shareMsg;
    }
}
